import * as cabinetService from '../../services/cabinet/cabinet';
import {queryAssetType} from '../../services/assetType';
import {queryAssetBrand,queryAssetModelAll, create} from '../../services/assetModel';
import {queryRoomList} from '../../services/tRoom';

export default {
  namespace: 'cabinet',
  state: {
    list: [],
    roomList:[],
    current : 1,
    currentSize : 10,
    total : 0,
    modalVisible : false,
    modalType : 'create',
    newKey : '',
    cabinetItem:{},
    rackTypeList:[],
    rackBrandList:[],
    rackModelList:[],
    cabinetGroupList:[],
    cabinetExtList:[],
  },
  reducers: {
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
   
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },

    qryCabinetExtListSuc(state, { payload: cabinetExtList }) {
      return { ...state, cabinetExtList };
    },

    queryRackBransListSuc(state, { payload: rackBrandList }) {
      return { ...state, rackBrandList };
    },

    queryRackTypeListSuc(state, { payload: rackTypeList }) {
      return { ...state, rackTypeList };
    },
    queryRackModelListSuc(state, { payload: rackModelList }) {
      return { ...state, rackModelList };
    },

    queryCabinetGroupListSuc(state, { payload: cabinetGroupList }) {
      return { ...state, cabinetGroupList };
    },
   
    showModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state,{payload}) {
      return { ...state,...payload, modalVisible: false,rackModelList:[] ,newKey : new Date().getTime() + '' }
    },

  },
  effects: {
    *queryRoom({ payload },{ call, put }) {
      const { body } = yield call(queryRoomList,payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    },
    
    *queryList({ payload },{ call, put }) {
      const { body } = yield call(cabinetService.queryList,payload);
      yield put({ type: 'queryListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
    },
    *queryRackTypeList({ payload },{ call, put }) {
      const { body } = yield call(cabinetService.queryRackTypeList,payload);
      yield put({ type: 'queryRackTypeListSuc', payload:body.data});
    },

    *qryCabinetExtList({ payload },{ call, put }) {
      const { body } = yield call(cabinetService.qryCabinetExtList,payload);
      yield put({ type: 'qryCabinetExtListSuc', payload:body.data});
    },

    *queryCabinetGroupList({ payload },{ call, put }) {
      const { body } = yield call(cabinetService.queryCabinetGroupList,payload);
      yield put({ type: 'queryCabinetGroupListSuc', payload:body.data});
    },

    *queryRackBrandList({ payload },{ call, put }) {
      const { body } = yield call(queryAssetBrand,payload);
      yield put({ type: 'queryRackBransListSuc', payload:body.data});
    },

    *queryRackModelList({ payload },{ call, put }) {
      const { body } = yield call(cabinetService.queryRackModelList,payload);
      yield put({ type: 'queryRackModelListSuc', payload:body.data});
    },
    *showAddModal({ payload },{ call, put }){
      yield put({ type: 'qryCabinetExtList', payload:{roomId:""}});
      yield put({ type: 'showModal', payload:{cabinetItem:payload.detail,modalType:payload.modalType}});
    },
    *showEditModal({ payload },{ call, put }){
      yield put({ type: 'showModal', payload:{cabinetItem:payload.detail,modalType:payload.modalType}});
      yield put({ type: 'qryCabinetExtList', payload:{roomId:payload.detail.roomId}});
      yield put({ type: 'queryRackModelList',payload:{ rackTypeId: payload.rackTypeId_, rackBrandId:payload.rackBrandId_}});
    },
    *create({ payload,resetFields },{ call, put }){
      const { body } = yield call(cabinetService.create,payload);
      if(body.httpCode==200){
        resetFields()
        yield put({ type: 'hideModal' });
        yield put({ type: 'reload' });
      }     
    },
    *edit({ payload, resetFields },{ call, put }){
      const { body } = yield call(cabinetService.edit,payload);
      if(body.httpCode==200){
        resetFields()
        yield put({ type: 'hideModal' });
        yield put({ type: 'reload' });
      }     
    },
    *remove({ payload },{ call, put }){
      const { body } = yield call(cabinetService.remove,payload.id);
      yield put({ type: 'queryList', payload:payload.search });
    },
    *reload(action, { put }) {
      yield put({ type: 'queryList', payload:{ 'name':'' }});
    },
  },

  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/cabinet') {
          dispatch({ type: 'queryRoom' });
          dispatch({ type: 'queryList', payload:{ 'status':'' }});
          dispatch({ type: 'queryRackTypeList'});
          dispatch({ type: 'queryCabinetGroupList'});
          dispatch({ type: 'queryRackBrandList',payload:{ 'name':'' }});
        }
      });
    },
  },
};
